package uc.org.jkas.app.admin;
import java.util.HashMap;
import java.util.ArrayList;
import uc.org.jkas.model.PrivilegesM;
import org.jkas.utils.Page;
public class Privileges extends BackBase
{ PrivilegesM psm=new PrivilegesM();
	public void index()
    {  if(IS_POST())
		{
		}
		else
		{   HashMap<String,ArrayList> tree=psm.getPrivileges(null);
			assign("lists",tree.get("lists"));
			assign("parents",tree.get("parents"));
		    display();
		}
	}

	public void add()
	{
		if(IS_POST())
		{String res=JSON_RESULT_FAILED;
		 HashMap<String,Object> data=new HashMap<String,Object>();
	       String caption=P("caption");
		   String parent_id=P("parent_id");
		   if(!isEmpty(caption))
			{
	                data.put("caption",caption);
					data.put("parent_id",parent_id);
		            data.put("control",Plow("control"));
					data.put("action",Plow("action"));
		             if(psm.insert(data)>0)
			           {res=JSON_RESULT_SUCCESS;
			           }
			}
			else
			{res="{\"code\":401,\"msg\":\"empty name\"}";
			}
		  echo(res);
		}
		else
		{   assign("parents",psm.where("parent_id=0").select());
			display();
		}
	}

	public void edit()
	{  int privi_id=Pint("privi_id");
		if(IS_POST())
		{ 
			String res=JSON_RESULT_FAILED;
			HashMap<String,Object> data=new HashMap<String,Object>();
	      String caption=P("caption");
		   String parent_id=P("parent_id");
           if(!isEmpty(caption))
			{       data.put("caption",caption);
					data.put("parent_id",parent_id);
		            data.put("control",Plow("control"));
					data.put("action",Plow("action"));
					data.put("privi_id",privi_id);
					 if(psm.update(data)>0)
			            {res=JSON_RESULT_SUCCESS;
			            }
			}
			else
			{res="{\"code\":401,\"msg\":\"empty name\"}";
			}
			echo(res);
		}
		else
		{   ArrayList<HashMap> parents=psm.where("parent_id=0").select();
		    assign("parents",parents);
			HashMap<String,String> info=psm.getInfo(privi_id);
			assign("info",info);
			display();
		}
	}

	public void drop()
	{String res=JSON_RESULT_FAILED;
	  if(IS_POST())
		{
		 int id=Pint("id");
		 String ids=P("ids");
	     if((id>0 && psm.where(strc("privi_id=",id," or parent_id=",id)).delete()>0) || (!isEmpty(ids) && psm.where(strc("privi_id in(",ids,") or parent_id in(",ids,")")).delete()>0))
		 {res=JSON_RESULT_SUCCESS;
		 }
		}
		echo(res);
	}

}